package com.xiaoyu.izf.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.MerchantInfo;
import com.xiaoyu.izf.model.MerchantShopInfo;
import com.xiaoyu.izf.model.Region;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.model.page.Status;
import com.xiaoyu.izf.service.MerchantInfoService;
import com.xiaoyu.izf.service.MerchantShopInfoService;
import com.xiaoyu.izf.service.RegionService;
import com.xiaoyu.izf.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/shop")
public class MerchantShopInfoController extends BaseController {

    @Autowired
    private MerchantShopInfoService merchantShopInfoService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private MerchantInfoService merchantInfoService;

    /**
     * 门店列表-合伙人
     * @param request
     * @return
     */
    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parListPage(HttpServletRequest request){
        return "mgr/shop/par_list";
    }
    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(HttpServletRequest request, @RequestBody String body) throws IOException {
        UserInfo user = user(request);
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantShopInfoService.list(paramListBean,user.getId());
        return dataTableBean;
    }

    /**
     * 门店列表-admin
     * @param request
     * @return
     */
    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage(HttpServletRequest request){
        return "mgr/shop/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantShopInfoService.list(paramListBean,null);
        return dataTableBean;
    }

    /**
     * 门店列表-当前商户名下
     * @return
     */
    @RequestMapping("/cur_merchant_list_page")
    @RequestAuth
    public String curMerchantListPage(int merchantId,Model model){
        model.addAttribute("merchantId",merchantId);
        return "mgr/shop/cur_merchant_list";
    }
    @RequestMapping("/cur_merchant_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean curMerchantList(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantShopInfoService.list(paramListBean,null);
        return dataTableBean;
    }

    /**
     * 门店添加
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/add_page")
    @RequestAuth
    public String addPage(HttpServletRequest request, Model model,int merchantId,Integer parUid){
        if (parUid==null)
            parUid = user(request).getId();
        System.err.println("parUid:"+parUid);
        List<Region> provinces = regionService.provinces();
        model.addAttribute("provinces",provinces);
        model.addAttribute("merchantId",merchantId);
        model.addAttribute("parUid",parUid);
        return "mgr/shop/add";
    }
    @RequestMapping("/add")
    @ResponseBody
    @RequestAuth
    public FormResultBean add(HttpServletRequest request, int merchantId,Integer parUid,
                                  int provinceRegionId,int cityRegionId,MerchantShopInfo msi){
        FormResultBean fr = new FormResultBean(Status.HTTP_OK.value());

        MerchantInfo mi = merchantInfoService.findById(merchantId);
        if (mi==null)
            return fr.setType("error").setMessage("商户不存在");
        msi.setMerchantId(merchantId);

        if (merchantShopInfoService.findByName(msi.getShopName())!=null){
            return fr.setType("error").setMessage("店名已存在");
        }

        msi.setUid(parUid);

        Region province = regionService.findByRegionId(provinceRegionId);
        Region city = regionService.findByRegionId(cityRegionId);
        if (province==null){
            return fr.setType("error").setMessage("请选择省份");
        }
        if (city==null){
            return fr.setType("error").setMessage("请选择城市");
        }
        msi.setProvince(province.getRegionName());
        msi.setCity(city.getRegionName());
        msi.setRegionCode(city.getRegionCode());

        if (!(Validator.isMobile(msi.getShopPhone())||CommonUtil.isPhone(msi.getShopPhone()))){
            return fr.setType("error").setMessage("手机号或座机号格式不正确");
        }

        if (StrUtil.isNotBlank(msi.getShopPic())){
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
            msi.setShopPic(basePath+msi.getShopPic());
        }

        msi.setCreateTime(new Timestamp(System.currentTimeMillis()));
        merchantShopInfoService.save(msi);

        return fr.setType("success").setMessage("添加成功");
    }
}
